package com.example.controller;

import cn.hutool.json.JSONObject;
import com.example.common.Result;
import com.example.entity.*;
import com.example.entity.others.Test_Map_Question;
import com.example.entity.others.Test_User_Other;
import com.example.entity.others.User_Question;
import com.example.entity.others.User_Subject;
import com.example.service.ParentService;
import com.example.service.StudentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/parent")
public class ParentController {
    @Resource
    private ParentService parentService;

    //返回子女列表
    @PostMapping("/list/student")
    public Result listStudent(@RequestBody User user){
        return Result.success(parentService.listStudent(user));
    }
    //添加子女
    @PostMapping("/bind")
    public Result bind(@RequestBody Parent_Student parent_Student){
        return Result.success(parentService.bind(parent_Student));
    }
    //返回子女指定学科的试卷标题
    @PostMapping("/test/list")
    public Result testList(@RequestBody User_Subject userSubject){
        return Result.success(parentService.testList(userSubject));
    }

    //返回子女的指定试卷的题目
    @PostMapping("/question/all")
    public Result questionAll(@RequestBody Test_User_Other testUserOther){
        return Result.success(parentService.questionAll(testUserOther));
    }

    //AI生成试卷
    //用ai生成试卷题目，一整个test实体和uid,生成到父母那边
    @PostMapping("/create/ai")
    public Result createAi(@RequestBody Test_User_Other testUser){
        parentService.createAi(testUser);
        User_Subject userSubject = new User_Subject();
        userSubject.setSubject(testUser.getTest().getSubject());
        userSubject.setUid(testUser.getUser().getUid());
        return Result.success(parentService.testListParent(userSubject));
    }

    //人工组卷
    //人工组卷，返回数据库里面所有的这个学科相关知识点的题目 test和user
    @PostMapping("/human/question")
    public Result humanQuestion(@RequestBody Test_User_Other testUser){
        return Result.success(parentService.humanQuestion(testUser));
    }

    //人工组卷，生成试卷 需要test的tid和questionMap和user
    @PostMapping("/create/human")
    public Result createHuman(@RequestBody Test_Map_Question testMapQuestion){
        parentService.createHuman(testMapQuestion);
        User_Subject userSubject = new User_Subject();
        userSubject.setSubject(testMapQuestion.getTest().getSubject());
        userSubject.setUid(testMapQuestion.getUser().getUid());
        return Result.success(parentService.testListParent(userSubject));
    }

    //返回父母的试卷标题
    @PostMapping("/test/list/parent")
    public Result testListParent(@RequestBody User_Subject userSubject){
        return Result.success(parentService.testListParent(userSubject));
    }

    //返回父母的试卷的问题
    @PostMapping("/question/all/parent")
    public Result questionAllParent(@RequestBody Test_User_Other testUserOther){
        return Result.success(parentService.questionAllParent(testUserOther));
    }

    //用户保存答案（只保存不批改）
    @PostMapping("/do/homework")
    public Result doHomework(@RequestBody List<Question_Parent> questionParents){
        parentService.doHomework(questionParents);
        return Result.success();
    }

    //用户重置答案（只保存不批改）
    @PostMapping("/question/reset")
    public Result questionReset(@RequestBody JSONObject testUserOther){
        int tid = testUserOther.getInt("tid");
        int uid = testUserOther.getInt("uid");
        Test_User_Other testUserOther1 = new Test_User_Other();
        Test test = new Test();
        test.setTid(tid);
        User user = new User();
        user.setUid(uid);
        testUserOther1.setTest(test);
        testUserOther1.setUser(user);
        parentService.questionReset(tid,uid);
        return Result.success(parentService.questionAllParent(testUserOther1));
    }

    //返回子女指定学科分析列表
    @PostMapping("/analysis/list")
    public Result analysisList(@RequestBody User_Subject userSubject){
        return Result.success(parentService.getAnalysis(userSubject));
    }
    //删除指定试卷 需要uid，tid
    @PostMapping("/test/delete")
    public Result testDelete(@RequestBody Test_User testUser){
        return Result.success(parentService.testDelete(testUser));
    }
    //用户提交试卷之后自动批改 需要问题的id,content,answer,uanswer
    @PostMapping("/correcting/ai")
    public Result correctingAi(@RequestBody List<User_Question> userQuestions){
        return Result.success(parentService.correctingAi(userQuestions));
    }
    //分发试卷
    @PostMapping("/set/test")
    public Result SetTest(@RequestBody Test_User testUser){
        parentService.setTest(testUser);
        return Result.success();
    }
    //返回学生学习情况
    @PostMapping("/student/analysis1")
    public Result studentAnalysis1(@RequestBody User_Subject userSubject){
        Map<String,List<JSONObject>> result = new HashMap<>();
        List<JSONObject> listPIE = new ArrayList<>();
        //试卷知识点分布图
        listPIE = parentService.studentAnalysis1(userSubject);
        result.put("PIE",listPIE);
        return Result.success(result);
    }
    @PostMapping("/student/analysis2")
    public Result studentAnalysis2(@RequestBody User_Subject userSubject){
        //知识点正确率分布图、知识点正确率折线图(最近十份)
        Map<String,List<Object>> result = parentService.studentAnalysis2(userSubject);
        return Result.success(result);
    }
    @PostMapping("/student/analysis3")
    public Result studentAnalysis3(@RequestBody User_Subject userSubject){
        //知识点正确率折线图(最近十份)
        Map<String,List<Object>> result = parentService.studentAnalysis3(userSubject);
        return Result.success(result);
    }@PostMapping("/student/analysis4")
    public Result studentAnalysis4(@RequestBody User_Subject userSubject){
        //习题完成情况
        Map<String,List<Object>> result = parentService.studentAnalysis4(userSubject);
        return Result.success(result);
    }
}
